package com.yenlien.traceyun.control.controller;

import com.yenlien.traceyun.control.entity.Advert;
import com.yenlien.traceyun.control.service.AdvertService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jialong
 * @date 2021/04/27
 * @description: 广告控制层
 **/
@RestController
@RequestMapping("/advert")
public class AdvertController extends BaseController {

    @Autowired
    private AdvertService advertService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@Validated Advert advert, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        if (advertService.save(advert, files))
            return success("success");
        else
            return fail("fail");
    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@Validated @RequestBody Advert advert) {
        if (advertService.save2(advert))
            return success("success");
        else
            return fail("fail");
    }


    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    public Object findList(@RequestBody Advert advert) {
        return advertService.findList(advert);
    }


    @GetMapping(value = "/findById/{id}")
    public Object findById(@PathVariable("id") String id) {
        return advertService.findById(id);
    }


    @DeleteMapping(value = "/deletes")
    public Object findList(String[] ids) {
        advertService.deletes(ids);
        return success("success");
    }

    /**
     * 广告的接收方获取消息
     *
     * @return
     */
    @GetMapping(value = "/getByCompanyId/{companyId}")
    public Object getAdvert(@PathVariable("companyId") String companyId) {
        return advertService.getAdvert(companyId);
    }


}
